package com.junzhiit.nengji.cms.web.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.cms.service.FunctionService;
import com.junzhiit.nengji.cms.web.vo.FunctionListVO;
import com.junzhiit.nengji.cms.web.vo.FunctionVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.model.Function;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/function")
public class FunctionController extends BaseController {

	@Autowired
	private FunctionService functionService;

	@ApiOperation(value = "获取功能权限列表")
	@RequestMapping(value = "/{platform}", method = RequestMethod.GET)
	public ResultVO findByPlatform(@PathVariable int platform) {
		AssertUtil.notNull(platform, "platform is required.");
		return functionService.findByPlatform(platform);
	}

	@ApiOperation(value = "获取角色功能权限列表")
	@RequestMapping(value = "/role", method = RequestMethod.GET)
	public ResultVO findByPlatformWithRole(FunctionListVO functionListVO) {
		return functionService.findBySystemUserId(functionListVO);
	}

	@ApiOperation(value = "创建权限")
	@RequestMapping(method = RequestMethod.POST)
	public ResultVO add(FunctionVO functionVO) {
		AssertUtil.notBlank(functionVO.getName(), "name is required.");
		Function function = new Function();
		BeanUtils.copyProperties(functionVO, function);
		return functionService.add(function);
	}

	@ApiOperation(value = "修改权限")
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	public ResultVO update(@PathVariable String id, FunctionVO functionVO) {
		AssertUtil.notBlank(functionVO.getName(), "name is required.");
		Function function = new Function();
		BeanUtils.copyProperties(functionVO, function);
		return functionService.edit(function);
	}

	@ApiOperation(value = "删除权限")
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public ResultVO delete(@PathVariable String id) {
		AssertUtil.notBlank(id, "id is required.");
		return functionService.delete(id);
	}

}
